package sim.states;

import ctf.network.ProtocolError;
import sim.ISimulator;
import sim.ISimulator.State;
import utils.CTFGrammar;
/**
 * State responsible for the managing of hand-shaking requests sent from clients.
 * @author N. Bhatia, A. Butt, N. Evans, C. Levesque, P. OKeefe
 *
 */
public class StartState extends AbstractState {
	public StartState(ISimulator simulator) {
		super(simulator);
	}

	@Override
	public void request(int clientId, String request) throws ProtocolError {
		String[] splitString = request.split(" ");
		if ((splitString.length == 2) && (splitString[0].equals(CTFGrammar.CTF2009))
				&& this.checkColor(splitString[1])) {
			this.simulator.setState(clientId, State.VERSION);
			this.simulator.respond(clientId, CTFGrammar.CTF2009);
		} else {
			throw new ProtocolError(
					CTFGrammar.CTF2009 + " <" + CTFGrammar.RED + "|"
					+ CTFGrammar.BLUE + "|" + CTFGrammar.GREEN + "|"
					+ CTFGrammar.PURPLE + "|"+ CTFGrammar.ORANGE + "|"
					+ CTFGrammar.BLACK + ">", request);
		}
	}

	private boolean checkColor(String string) {
		if (string.equals(CTFGrammar.RED) || string.equals(CTFGrammar.BLUE)
				|| string.equals(CTFGrammar.GREEN) || string.equals(CTFGrammar.PURPLE)
				|| string.equals(CTFGrammar.ORANGE) || string.equals(CTFGrammar.BLACK)) {
			return true;
		} else {
			return false;
		}
	}
}
